package com.beclub.zxing;

import android.app.Activity;
import android.content.Intent;

public class CodeScanning {
	public static final int SCAN_RESULT_REQUEST = 10000;
	private static final CodeScanning instance = new CodeScanning(); 

	public interface CodeScanningCallback {
		public void onCodeScanned(String codeNumber, Integer codeType);

		public void onCodeFailed();
	}

	private CodeScanningCallback mCallback;

	public CodeScanning() {

	}

	/**
	 * 开始扫码，并提供回调
	 * 
	 * @param activity
	 * @param callback
	 */
	public void startScanning(Activity activity, CodeScanningCallback callback) {
		try { 
			mCallback = callback;

			Intent intent = new Intent(activity,
					com.beclub.zxing.MipcaActivityCapture.class);
			activity.startActivityForResult(intent, SCAN_RESULT_REQUEST);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 扫码之后执行处理的方法
	 * 
	 * @param resultCode
	 * @param data
	 */
	public void scanResultHandler(int resultCode, Intent data) {
		if (resultCode == -1) {
			// Scan result is available by making a call to
			// data.getStringExtra(ZBarConstants.SCAN_RESULT)
			// Type of the scan result is available by making a call to
			// data.getStringExtra(ZBarConstants.SCAN_RESULT_TYPE)
			// 扫码成功回调 参数1.scan_result扫码的内容 2.扫码的类型
			mCallback.onCodeScanned(data
					.getStringExtra(com.beclub.zxing.ZxingConstants.SCAN_RESULT),
					data.getIntExtra(
							com.beclub.zxing.ZxingConstants.SCAN_RESULT_TYPE, 0));

			// The value of type indicates one of the symbols listed in Advanced
			// Options below.
		} else if (resultCode == 0) {
			// 扫码失败回调
			mCallback.onCodeFailed();
		}
	}

	public static CodeScanning getInstance() {
		return instance;
	}
}
